<template>
  <el-breadcrumb separator="/" class="mb-2 mt-1" style="vertical-align: bottom">
    <el-breadcrumb-item>
      <div class="flex align-center">
        <el-icon :size="18"> <DataAnalysis /> </el-icon
        ><span class="pl">報表管理</span>
      </div>
    </el-breadcrumb-item>
    <el-breadcrumb-item>购物车報表</el-breadcrumb-item>
  </el-breadcrumb>
  <el-card class="box-card" shadow="hover">
    <template #header>
      <div class="flex justify-between flex-wrap">
        <el-space wrap :size="10" class="mt">
          <el-date-picker
            v-model="myDate"
            type="daterange"
            range-separator="To"
            start-placeholder="開始日期"
            end-placeholder="結束日期"
            :editable="false"
            :disabled-date="disabledDateFun"
          />
          <el-input
            v-model="params.user_name"
            style="width: 150px"
            placeholder="用户名称"
          />
           <el-input
            v-model="params.goods_name"
            style="width: 150px"
            placeholder="商品名称"
          />
          <el-select
            v-model="params.more_cart_id"
            placeholder="多重購物車"
            style="width: 150px"
          >
            <el-option
              v-for="item in filter.morcart"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
          <el-select
            v-model="params.source_id"
            placeholder="来源"
            style="width: 150px"
            clearable
          >
            <el-option
              v-for="item in filter.source"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
          <el-button :icon="Search" plain @click="search">查詢</el-button>
        </el-space>
        <el-space wrap :size="10" class="mt">
          <el-button type="primary" plain>下载</el-button>
          <el-button type="primary" plain>图表</el-button>
        </el-space>
      </div>
    </template>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="date" label="日期" width="120" />
      <el-table-column label="用户名称/PSID" min-width="120">
        <template #default="scope">
          <div>{{ scope.row.user_name }}</div>
          <div>{{ scope.row.PSID }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="goods_name" label="商品名称" />
      <el-table-column prop="goods_desc" label="商品描述" />
      <el-table-column prop="goods_price" label="商品售价" />
      <el-table-column prop="goods_num" label="数量" />
      <el-table-column prop="source" label="来源" />
      <el-table-column prop="more_cart" label="多重购物车" />
      <el-table-column prop="sum" label="小计" />
    </el-table>
  </el-card>
</template>
<script lang="ts" setup>
import { ref, onMounted } from "vue";
import { Search, DataAnalysis } from "@element-plus/icons-vue";
import { $cartList, $source } from "@api/report";
import { $morcart } from "@api/live";
import { dayjs } from "element-plus";
let params = ref({
  current_page: 1,
  page_size: 10,
  goods_name: "",
  user_name: "",
  source_id: "",
  more_cart_id: "",
  start_time: "",
  end_time: "",
});
const total = ref(0);
const tableData = ref([]);
const filter = ref({
  source: [],
  morcart: [],
});
let myDate = ref([]);
onMounted(() => {
  getFilter();
});
const getFilter = async () => {
  const source_res = await $source();
  const morcart_res = await $morcart();
  filter.value.source = source_res.data.list;
  filter.value.morcart = morcart_res.data.list;
  //初始化默认值
  params.value.more_cart_id = morcart_res.data.list[0].id;
  getData();
};
const getData = async () => {
  if (myDate.value.length != 0) {
    params.value.start_time = dayjs(myDate.value[0]).format("YYYY-MM-DD");
    params.value.end_time = dayjs(myDate.value[1]).format("YYYY-MM-DD");
  }
  let res = await $cartList(params.value);
  console.log(res);
  if (res.code == 0) {
    tableData.value = res.data.table;
    total.value = res.data.total;
  }
};
const handleSizeChange = (val: number) => {
  params.value.page_size = val;
  getData();
  console.log(`${val} items per page`);
};
const handleCurrentChange = (val: number) => {
  params.value.current_page = val;
  getData();
  console.log(`current page: ${val}`);
};
const search = () => {
  console.log(params.value);
  getData();
};
const handleClick = (tab: TabsPaneContext, event: Event) => {
  console.log(params);
  getData();
};
const disabledDateFun = (date) => {
  let now = dayjs().subtract(1, "day");
  if (date < now) return false;
  return true;
};
</script>

<style lang="scss" scoped></style>
